package cn.jinjimi.app.custom.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.BundleKey;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.iview.CustomTypeListView;
import cn.jinjimi.app.custom.ui.adapter.CustomerTypeAdapter;
import cn.jinjimi.app.custom.ui.view.CustomSoldOrUnsoldGuideView;
import cn.jinjimi.app.custom.utils.LetterUtil;
import cn.jinjimi.app.presenter.CustomerTypePresenter;
import cn.jinjimi.app.main.dialog.CommMenuDialog;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;
import cn.jinjimi.app.main.widget.XListView.XListView;
import cn.jinjimi.app.util.ToastUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 分类显示客户
 * <p/>
 * Created by jinsen on 15/11/30.
 */
public class CustomerTypeListActivity extends ToolBarCommonActivity implements
        CustomTypeListView, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    //客户的类型
    private int mCustomType = Custom.TYPE_UNSOLD_CUSTOMER;

    private CustomSoldOrUnsoldGuideView mGuideView;

    private RelativeLayout mRootView;

    private XListView mListView;

    private CustomerTypeAdapter mAdapter;
    private CustomerTypePresenter mPresenter;

    public static void lanch(Context context, int type) {
        Intent intent = new Intent();
        intent.setClass(context, CustomerTypeListActivity.class);
        intent.putExtra(BundleKey.TYPE, type);
        context.startActivity(intent);
    }

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_customer_type_list_layout);
        mCustomType = getIntent().getIntExtra(BundleKey.TYPE, Custom.TYPE_UNSOLD_CUSTOMER);

        if (mCustomType == Custom.TYPE_UNSOLD_CUSTOMER) {
            setTitle(getString(R.string.customer_type_unsold_name));
        } else if (mCustomType == Custom.TYPE_SOLDED_CUSTOMER) {
            setTitle(getString(R.string.customer_type_solded_name));
        }
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        mRootView = (RelativeLayout) findViewById(R.id.root_view);
        mListView = (XListView) findViewById(R.id.type_customer_listview);
        mListView.setPullLoadEnable(false);
        mListView.setPullRefreshEnable(false);
        mListView.setTransparentBg();
    }

    protected void initListener() {
        mListView.setOnItemClickListener(this);
        mListView.setOnItemLongClickListener(this);
    }

    protected void initData() {
        mAdapter = new CustomerTypeAdapter(this);
        mListView.setAdapter(mAdapter);
        mPresenter = new CustomerTypePresenter(this, mCustomType);
        mPresenter.requestData();
    }

    private void showGuideView() {
        if (mRootView == null) return;
        if (mGuideView == null) {
            mGuideView = new CustomSoldOrUnsoldGuideView(CustomerTypeListActivity.this);
            mGuideView.setType(mCustomType);
        }

        mRootView.removeView(mGuideView);
        mRootView.addView(mGuideView, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    private void hidGuideView() {
        if (mGuideView != null)
            mRootView.removeView(mGuideView);
    }

    @Override
    public void setCustomers(List<Custom> customers) {
        mAdapter.setItems(customers);
        if (customers.size() > 0) {
            mListView.setVisibility(View.VISIBLE);
            hidGuideView();
        } else {
            mListView.setVisibility(View.GONE);
            showGuideView();
        }
    }

    @Override
    public void updateTypeSucc(Custom custom) {
        String msg;
        switch (custom.type) {
            case Custom.TYPE_UNSOLD_CUSTOMER:
                msg = getString(R.string.custoemr_type_unsold_suc);
                break;
            case Custom.TYPE_SOLDED_CUSTOMER:
                msg = getString(R.string.customer_type_sold_suc);
                break;
            default:
                msg = getString(R.string.customer_type_cancle);
                break;
        }
        ToastUtils.toastMessage(this, msg);
        mAdapter.remove(custom);
    }

    @Override
    public void justLoading() {
        ToastUtil.showLoading(CustomerTypeListActivity.this, "");
    }

    @Override
    public void resortCustoms() {
        Collections.sort(mAdapter.getItems(), new LetterUtil.ComparatorTop());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void titleAction() {
        super.titleAction();
        if (mListView != null)
            mListView.smoothScrollToPosition(0);
    }

    @Override
    public void loading() {
        ToastUtil.showLoading(CustomerTypeListActivity.this, getResources().getString(R.string.loading_text));
    }

    @Override
    public void loadSucc() {
        hideLoadingIndicator();
    }

    @Override
    public void loadFail() {
        hideLoadingIndicator();
    }

    @Override
    public void showToast(int resId) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int index = position - mListView.getHeaderViewsCount();
        if (index > -1) {
            Custom custom = mAdapter.getItem(index);
            if (custom != null)
                CustomArchivesActivity.launch(this, custom);
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        int index = position - mListView.getHeaderViewsCount();
        if (index > -1) {
            Custom custom = mAdapter.getItem(index);
            if (custom == null)
                return true;

            switch (mCustomType) {
                case Custom.TYPE_UNSOLD_CUSTOMER:
                    showUnsoldCustomerMenu(custom);
                    break;

                case Custom.TYPE_SOLDED_CUSTOMER:
                    showSoldedCustomerMenu(custom);
                    break;
            }

        }
        return true;
    }

    //显示标准客户的悬浮菜单
    private void showUnsoldCustomerMenu(final Custom custom) {
        final CommMenuDialog dialog = new CommMenuDialog(this);
        ArrayList<String> menu = new ArrayList<String>();
        menu.add(getString(R.string.custoemr_type_flag_sold));
        if (custom.hasTop()) {
            menu.add(getString(R.string.custoemr_top_cancle));
        } else {
            menu.add(getString(R.string.customer_top));
        }
        dialog.setMenu(menu);
        dialog.setMenuItemLisener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        mPresenter.setCustomerType(custom, CustomerTypeListActivity.this);
                        break;
                    case 1:
                        mPresenter.setCustomerTopState(custom, CustomerTypeListActivity.this);
                        break;
                }
                dialog.dismiss();
            }
        });
        dialog.show();
    }


    //显示标准客户的悬浮菜单
    private void showSoldedCustomerMenu(final Custom custom) {
        final CommMenuDialog dialog = new CommMenuDialog(this);
        ArrayList<String> menu = new ArrayList<String>();
        if (custom.hasTop()) {
            menu.add(getString(R.string.custoemr_top_cancle));
        } else {
            menu.add(getString(R.string.customer_top));
        }
        dialog.setMenu(menu);
        dialog.setMenuItemLisener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mPresenter.setCustomerTopState(custom, CustomerTypeListActivity.this);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
        mPresenter = null;
    }

}